package com.bjc.core.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bjc.base.bean.ResultVO;
import com.bjc.base.core.ResultGenerator;
import com.bjc.core.bean.request.EmployeeReqVO;
import com.bjc.core.bean.request.BaseUserReqVO;
import com.bjc.core.bean.response.EmployeeVO;
import com.bjc.core.bean.response.BaseUserVO;
import com.bjc.core.service.IEmployeeService;
import com.bjc.core.service.IBaseUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 用户
 * @author 陈皓
 * @date 2017年8月10日 下午4:23:46
 */
@RestController
@RequestMapping("api/cloud/employee")
// @PermissionPath("系统/用户")
@Api(value = "user-api", tags = { "用户相关接口" })
public class EmployeeController {

	@Autowired
	private IBaseUserService userService;
	@Autowired
	private IEmployeeService employeeService;

	@ApiOperation("分页查询用户")
	@GetMapping("findPage")
	public ResultVO getUserPage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,
			EmployeeReqVO employeeReqVO) {
		PageHelper.startPage(pageNum, pageSize);
		return ResultGenerator.getSuccess(new PageInfo<EmployeeVO>(employeeService.getList(employeeReqVO)));
	}

	@ApiOperation("获取当前登录用户")
	@GetMapping("findCurrent")
	public ResultVO getUserPage() {
//		Long currentWorkId = (Long) SecurityUtils.getSubject().getSession().getAttribute("currentWorkId");
//		System.out.println("获取当前登录用户"+currentWorkId);
		BaseUserVO userVO = new BaseUserVO();
		userVO.setUserAccount("123456");
		List<String> roleList = new ArrayList<>();
		roleList.add("systemAdmin");
		userVO.setRoleList(roleList);
		return ResultGenerator.getSuccess(userVO);
	}

	@ApiOperation("添加用户")
	@PostMapping("create")
	public ResultVO createUser(@RequestBody EmployeeReqVO employeeReqVO) {
		int count = employeeService.insert(employeeReqVO);
		return ResultGenerator.getInsertSuccess(count);
	}

	@ApiOperation("更新用户")
	@PutMapping("update")
	public ResultVO updateUser(@RequestBody BaseUserReqVO userReqVO) {
		int count = userService.update(userReqVO);
		return ResultGenerator.getUpdateSuccess(count);
	}

	@ApiOperation("删除用户")
	@DeleteMapping("delete/{id}")
	public ResultVO deleteUserById(@ApiParam(value = "用户id", required = true) @PathVariable("id") Integer id) {
		int count = userService.deleteById(id);
		return ResultGenerator.getDeleteSuccess(count);
	}
}
